/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wuhenzhizao.ohostitlebar.utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import org.wuhenzhizao.ohostitlebar.ResourceTable;

import java.util.ArrayList;
import java.util.List;

public final class ViewCreateHelper {
    private static final int NOTE_NUMBER_7 = 7;
    private Context slice;

    public ViewCreateHelper(Context abilitySlice) {
        this.slice = abilitySlice;
    }

    public Component createView(String title, int position) {
        Component mainComponent;
        mainComponent = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_pager_item, null, false);
        if (!(mainComponent instanceof ComponentContainer)) {
            return null;
        }
        Text mText = (Text) mainComponent.findComponentById(ResourceTable.Id_List_text2);
        mText.scrollTo(0, 0);
        mText.enableScrollBar(1, true);
        initView(mainComponent, position);

        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        return rootLayout;
    }

    private void initView(Component mainComponent, int position) {
        ScrollView contentLayout = (ScrollView) mainComponent.findComponentById(ResourceTable.Id_content_layout);
        List<Note> notes = generateNotes(NOTE_NUMBER_7);
        for (int i = 0; i < notes.size(); i++) {
            Component component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_layout_text_item, null, false);
            Note noteModel = notes.get(i);

            String color = noteModel.getColor();

            Element element = buildBg(Color.getIntColor(color), 0);
            component.setBackground(element);
            contentLayout.addComponent(component);
        }
    }

    private Element buildBg(int color, float radius) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(0);
        drawable.setRgbColor(RgbColor.fromArgbInt(color));
        drawable.setCornerRadius(radius);
        return drawable;
    }

    /**
     * 生成数据
     *
     * @param numNotes 列表展示的个数
     * @return 数据集合
     */
    private List<Note> generateNotes(int numNotes) {
        List<Note> notes = new ArrayList<>(numNotes);
        return notes;
    }
}
